package com.tliaswebmanagement.controller;

import com.tliaswebmanagement.pojo.Emp;
import com.tliaswebmanagement.pojo.Result;
import com.tliaswebmanagement.service.EmpService;
import com.tliaswebmanagement.utlis.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@Slf4j
public class LoginController {
    @Autowired
    private EmpService empService;

    /**
     * 登录
     *
     * @param emp
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody Emp emp) {
        log.info("登陆的账号密码:{}", emp);
        Emp e = empService.login(emp);
        if (e != null) {
            Map<String, Object> mp = new HashMap<>();
            mp.put("id", e.getId());
            mp.put("username", e.getUsername());
            String jwt = JwtUtils.generateJwt(mp);
            return Result.success(jwt);
        }
        return Result.error("用户名错误");
    }
}
